package com.vpx.server.utils;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipUtils {

	public static void main(String[] s) {
//		ZipFiles(new File("D:\\attach"),"D:\\zip\\1.zip");
	}
	public static int zipFiles(File folder, OutputStream os) {
		try {
			BufferedOutputStream bs = new BufferedOutputStream(os);
			ZipOutputStream out = new ZipOutputStream(bs);
			zip(folder, out);
			out.closeEntry();
			out.close();
			return 1;
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		}
	}
	public static int zipFiles(File folder, String zipFile) {
		try {
			// Create the ZIP file
			OutputStream os = new FileOutputStream(zipFile);
			BufferedOutputStream bs = new BufferedOutputStream(os);
			ZipOutputStream out = new ZipOutputStream(bs);
			zip(folder, out);
			out.closeEntry();
			out.close();
			return 1;
		} catch (IOException e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	public static void zip(File folder,ZipOutputStream zo) throws IOException {
        File[] files = folder.listFiles();
        byte[] buf = new byte[1024];
        int len;
        for(int i=0; i<files.length; i++) {
            FileInputStream fin = new FileInputStream(files[i]);
            zo.putNextEntry(new ZipEntry(files[i].getName()));
            while((len=fin.read(buf))>0) {
                zo.write(buf,0,len);
            }
            zo.flush();
            fin.close();
        }
	}
}
